<?php

class PressPaper extends PressDirModel
{
	
//	public $groupName='press.ut_press_name';
	public $colorityText;
	
	public function getColorities()
	{
		$result=array();
		foreach ($this->pp_colorities as $colority){
			$result[]=$colority->ut_colority_id;
		}
		return $result;// $this->findAllByAttributes();
	}
	
	public function setColorities($value)
	{
		if (is_array($value)){
			CActiveRecord::model('PressPaperColority')->deleteAllByAttributes(array('ut_press_id'=>$this->ut_press_id,'ut_paper_id'=>$this->ut_paper_id));
			foreach ($value as $key){
				$record=new PressPaperColority();
				$record->ut_press_id=$this->ut_press_id;
				$record->ut_paper_id=$this->ut_paper_id;
				$record->ut_colority_id=$key;
				$record->save();
			}
		}
	}
	
	
	public function getViewColumns()
	{
		return array(
			'press.ut_press_name',
			'paper.paperLabel',
			'colorityText',
		);
	}
	
	public function getUpdateFields()
	{
		switch (Yii::app()->controller->action->id)
		{
		case 'create':
			$result=array(
				array(
					'name'=>'ut_press_id',
					'editor'=>'ajaxDropDown',
					'data'=>CActiveRecord::model('Press')->listData(),
				//	'replace'=>'#'.CHtml::activeId($this,'ut_paper_id'),
				),
				array(
					'name'=>'ut_paper_id',
					'editor'=>'dropDown',
					'data'=>(Yii::app()->controller->action->id=='create')?
								CActiveRecord::model('Paper')->absence(array('press'=>$this->ut_press_id))->listData('ut_paper_id','paperLabel'):
								CActiveRecord::model('Paper')->listData('ut_paper_id','paperLabel'),
				),
			);
			break;
		case 'update':
			$result=array(
				array(
					'name'=>'colorities',
					'editor'=>'checkBoxGroupList',
					'data'=>array('Все'=>CActiveRecord::model('Colority')->listData('ut_colority_id','ut_colority_name','colorGroup')),
				)
			);
			break;
		default: 
			$result=array();
			break;
		}
		return $result;
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ut_press_paper';
	}

	public function defaultScope()
	{
		return array(
			'select'=>'t.*,GROUP_CONCAT(uc.ut_colority_name) as colorityText',
			'group'=>'t.ut_press_id,t.ut_paper_id',
			'join'=>'LEFT OUTER JOIN ut_press_paper_colority uppc ON uppc.ut_press_id=t.ut_press_id AND uppc.ut_paper_id=t.ut_paper_id LEFT OUTER JOIN ut_colority uc ON uc.ut_colority_id=uppc.ut_colority_id AND uc.ut_colority_base',
			'order'=>'t.ut_press_id,paper.ut_paper_kind_id,density.ut_density_value,uppc.ut_colority_id',
			'with'=>array('paper','paper.density','press'),
		);
	}
	
	public function scopes()
	{
		return array(
			'press'=>array(
				'condition'=>'ut_press_id=:press',
			),
		);
	}	
	
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ut_press_id, ut_paper_id', 'required'),
			array('ut_press_id, ut_paper_id', 'type', 'type'=>'integer'),
			array('colorities', 'safe'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'paper'=>array(self::BELONGS_TO,'Paper','ut_paper_id'),
			'press'=>array(self::BELONGS_TO,'Press','ut_press_id'),
		//	'colority'=>array(self::BELONGS_TO,'Colority','ut_colority_id'),
			'pp_colorities'=>array(self::HAS_MANY,'PressPaperColority','ut_press_id,ut_paper_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ut_press_id' => 'Печатная машина',
			'ut_paper_id' => 'Бумага',
		);
	}


	
}